﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DoorControl
{
    public enum States
    {
        DoorClosed,
        DoorOpening,
        DoorOpen,
        DoorBreached
    }
    public class DoorController
    {
        ICardDB _cardDB;
        IDoor _door;
        IBeeper _beeper;
        IAlarm _alarm;

        public DoorController(ICardDB cardDB, IDoor door, IBeeper beeper, IAlarm alarm)
        {
            _cardDB = cardDB;
            _door = door;
            _beeper = beeper;
            _alarm = alarm;

        }

        

        public States _state = States.DoorClosed;

        public void DoorOpened()
        {
            switch (_state)
            {
                    case States.DoorOpening:
                        _state = States.DoorOpen;
                        _door.Close();
                        break;

                    case States.DoorClosed:
                        _door.Close();
                        _alarm.SoundAlarm();
                        _state = States.DoorBreached;
                        break;



            }
            
        }

        public void DoorClosed()
        {
            _state = States.DoorClosed;
        }

        public void RequestEntry(int id)
        {
            switch (_state)
            {
                case States.DoorClosed:

                    bool answer = _cardDB.ValidateEntryRequest(id);
                    if (answer)
                    {
                        _door.Open();
                        _beeper.MakeHappyNoise();
                        _state = States.DoorOpening;
                    }
                    else
                    {
                        _beeper.MakeUnhappyNoise();
                    }
                    break;


            }
            
        }

    }
}
